package SingleMachineArrangeImpl;

import java.util.Arrays;

/*2.1.2 总完工时间问题（有就绪时间）*/
//最早完工时间优先
public class ECT extends SingleMachineArrange{

    @Override
    public void solve() {
        if (process == null || ready == null) {
            process = new int[]{3, 18, 17, 21, 25};
            ready = new int[]{35, 22, 34, 37, 66};
        }
        int[] complete = new int[process.length];
        int[] s = new int[process.length];
        final int COMPLETE = -1;
        int t = 0;
        int n = process.length;
        for (int i = 0; i < n; i++) {

            int comp = Integer.MAX_VALUE;
            int comp_j = -1;
            for (int j = 0; j < n; j++) {
                //用0表示已安排
                if (process[j] == COMPLETE) continue;
                s[j] = Math.max(ready[j], t);
                complete[j] = s[j] + process[j];
                //取得最早完成工件的下标
                if (complete[j] < comp) {
                    comp = complete[j];
                    comp_j = j;
                }
            }
            t = complete[comp_j];
            process[comp_j] = COMPLETE;
            System.out.println("proc:" + Arrays.toString(process));
            System.out.println("comp:" + Arrays.toString(complete));
            System.out.println("comp_j = " + (comp_j + 1));
        }
        System.out.println(t);
    }
}
